﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TestStarter.Model;
using TestStarter.Util;

namespace TestStarter.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for TestsConfigurationPage.xaml
    /// </summary>
    public partial class TestsConfigurationPage : Page
    {
        private List<ExcelTestScript> OriginalTestList = new List<ExcelTestScript>();
        private List<string> GroupNameList = new List<string>();
        private List<string> TestScriptList = new List<string>();
        private ExcelDataAccess excelDA;
        private ExcelTestScript _excelTestScript = new ExcelTestScript();
        private int SelectedItemsCount = 0;

        public TestsConfigurationPage()
        {
            InitializeComponent();

            try
            {
                DirectoryInfo directory = new DirectoryInfo(ApplicationSettings.SCAN_FOLDER);
                FileInfo[] files = directory.GetFiles("*.xls");

                foreach (FileInfo fi in files)
                {
                    TestScriptList.Add(fi.Name);
                }

                TestScriptComboBox.ItemsSource = TestScriptList;
                TestScriptComboBox.Items.Refresh();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void UpdateSelectedItemCount()
        {
            SelectedCountLabel.Content = OriginalTestList.Where(t => t.IsRun.Equals(true)).ToList().Count;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                List<ExcelTestScript> SelectedTestSuiteList = new List<ExcelTestScript>();

                SelectedTestSuiteList = OriginalTestList.Where(t => t.IsChange.Equals(true)).ToList();

                if (SelectedTestSuiteList.Count != 0)
                {
                    bool IsSave = false;

                    foreach (ExcelTestScript ets in SelectedTestSuiteList)
                    {
                        IsSave = excelDA.InsertOrUpdateRowInExcelAsync(ets).Result;
                        OriginalTestList.Single(x => x.ID == ets.ID).IsChange = false;
                    }

                    if (IsSave)
                    {
                        MessageBox.Show("Record Update Successfully");
                    }
                    else
                    {
                        MessageBox.Show("Record Update Failed");
                    }
                }
                else
                {
                    MessageBox.Show("No Changes to Update");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void TestSuites_TargetUpdated(object sender, DataTransferEventArgs e)
        {
            DataGrid dg = (DataGrid)sender;
            ExcelTestScript changedItem = (ExcelTestScript)dg.SelectedItem;

            if (changedItem != null)
            {
                if (dg.SelectedIndex != -1)
                {
                    OriginalTestList.Single(x => x.ID == changedItem.ID).IsChange = true;
                }
            }
            UpdateSelectedItemCount();
        }

        private void TestSuites_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _excelTestScript = TestSuites.SelectedItem as ExcelTestScript;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            OriginalTestList = excelDA.GetDataFormExcelAsync().Result.ToList();
            TestSuites.ItemsSource = OriginalTestList;
            TestSuites.Items.Refresh();
            UpdateSelectedItemCount();
        }

        private void SelectAllCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var item in OriginalTestList)
            {
                if (!item.IsRun)
                {
                    var selected = OriginalTestList.Single(x => x.ID == item.ID);
                    selected.IsRun = true;
                    selected.IsChange = true;
                }
            }

            TestSuites.ItemsSource = OriginalTestList;
            TestSuites.Items.Refresh();
            UpdateSelectedItemCount();
        }

        private void SelectAllCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            OriginalTestList = excelDA.GetDataFormExcelAsync().Result.ToList();
            TestSuites.ItemsSource = OriginalTestList;
            TestSuites.Items.Refresh();
            UpdateSelectedItemCount();
        }

        private void GroupCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            var checkbox = sender as CheckBox;

            CheckList(checkbox.Content.ToString());
        }

        private void GroupCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            var checkbox = sender as CheckBox;
            UncheckList(checkbox.Content.ToString());
        }

        private void CheckList(string group)
        {
            List<ExcelTestScript> SelectedTestSuiteList = OriginalTestList.Where(t => t.Group.Equals(group)).ToList();

            foreach (var item in SelectedTestSuiteList)
            {
                if (!item.IsRun)
                {
                    var selected = OriginalTestList.Single(x => x.ID == item.ID);
                    selected.IsRun = true;
                    selected.IsChange = true;
                }
            }

            TestSuites.ItemsSource = OriginalTestList;
            TestSuites.Items.Refresh();
            UpdateSelectedItemCount();
        }

        private void UncheckList(string group)
        {
            List<ExcelTestScript> SelectedTestSuiteList = OriginalTestList.Where(t => t.Group.Equals(group)).ToList();

            foreach (var item in SelectedTestSuiteList)
            {
                var selected = OriginalTestList.Single(x => x.ID == item.ID);

                if (item.IsChange)
                {
                    selected.IsRun = false;
                    selected.IsChange = false;
                }
                else
                {
                    selected.IsRun = false;
                    selected.IsChange = true;
                }
            }

            TestSuites.ItemsSource = OriginalTestList;
            TestSuites.Items.Refresh();
            UpdateSelectedItemCount();
        }

        private void TestScriptComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectTestScriptTextBlock.Visibility.Equals(Visibility.Visible))
            {
                SelectTestScriptTextBlock.Visibility = TestScriptComboBox.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;
                ScriptDataGridPanel.Visibility = TestScriptComboBox.SelectedItem == null ? Visibility.Hidden : Visibility.Visible;
                ScriptDataGroupPanel.Visibility = TestScriptComboBox.SelectedItem == null ? Visibility.Hidden : Visibility.Visible;
            }

            var selecteditem = TestScriptComboBox.SelectedItem.ToString();

            excelDA = new ExcelDataAccess(selecteditem);
            GroupNameList = new List<string>();

            try
            {
                OriginalTestList = excelDA.GetDataFormExcelAsync().Result.ToList();
                TestSuites.ItemsSource = OriginalTestList;

                foreach (var ets in OriginalTestList)
                {
                    string item = GroupNameList.SingleOrDefault(t => t.Equals(ets.Group));

                    if (item == null)
                    {
                        GroupNameList.Add(ets.Group);
                    }
                }

                GroupNameListView.ItemsSource = GroupNameList;
                GroupNameListView.Items.Refresh();

                UpdateSelectedItemCount();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
